var app = getApp();;


var apiHost = app.globalData.applet_park_api_url;

var TOKEN = app.globalData.TOKEN || wx.getStorageSync("TOKEN");
var tokenKey = "token";
// 登录地址, 根据这个地址来设置token
var logInUrl = "/Account/LogInForMiniProgram";
// 例外不用token的地址
var exceptionAddrArr = [
  'http://localhost:8080',
];

/** 
 * @param url:String  require(必需) 请求地址相对路径
 * @param data:Object   可选  请求数据
 * @param success:Function  可选   成功回调函数
 * @param fail:Function     可选    失败回调函数
 */
function Request(url, data, method, success, fail) {
  CreateHeader(url, function(header) {
    wx.request({
      url: apiHost + url,
      method: method,
      data: data,
      header: header,
      success: function(res) {
        if (success && typeof success === "function") {
          success(res);
        } else {

        }
      },
      fail: function(error) {
        if (fail && typeof fail === "function") {
          fail(error);
        } else {
          
        }
      }
    })
  });
}

function requestDataparams(url, data, method, sign) {
  var that = this;
  let contentType=''
  let TOKEN = app.globalData.TOKEN || wx.getStorageSync("TOKEN");
  if(sign){
     contentType = 'application/json;charset=UTF-8'
  } else {
    contentType = 'application/x-www-form-urlencoded'

  }
  return new Promise(function(resolve, reject) {
    wx.request({
      url: apiHost + url,
      data: data,
      method: method,
      header: {
        'content-type': contentType,
        Authorization: TOKEN
      },
      success: function(res) {
    
        
        
        if(res.data.code==0){
          resolve(res)
        }else {
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }
      },
      fail: function(res) {
        reject(res);
        
        wx.showToast({
          title: '网络异常',
          icon:'none'
        })
      },
    })
  })
}
function postData(url, method) {
  var that = this;
  let TOKEN = app.globalData.TOKEN || wx.getStorageSync("TOKEN");
  return new Promise(function (resolve, reject) {
    wx.request({
      url: that.globalData.baseURL + url,
      method: method,
      header: {
        "Content-Type": "application/json;charset=UTF-8",
        "token": TOKEN
      },
      success: function (res) {
        reject(res);
      },
      fail: function (res) {
        reject(res);
      },
    })
  })
}
/** 
 * @param url:String  require(必需) 请求地址相对路径
 * @param data:Object   可选  请求数据
 * @param success:Function  可选   成功回调函数
 * @param fail:Function     可选    失败回调函数
 */


/** 
 * @param url:String    请求地址(根据请求地址判断是否添加token)
 * @param complete:Function 回调函数
 */
function CreateHeader(url, complete) {
  var header = {
    'content-type': 'application/json'
  }
  if (exceptionAddrArr.indexOf(url) == -1) { //排除请求的地址不需要token的地址
    wx.getStorage({
      key: tokenKey,
      success: function(res) {
        header.Authorization = 'Bearer ' + res.data;
      },
      fail: function(error) {
        
      },
      complete: function() {
        complete && typeof complete === 'Function' ? complete(header) : null;
      }
    });
  } else {
    complete && typeof complete === 'Function' ? complete(header) : null;
  }
}

module.exports = {
  requestDataparams: requestDataparams,
}